#!/bin/bash

if [ "$#" != "1" -a "$#" != "2" ]; then
    echo "$0 2024-2025-2-专业选修课程-2418320036-Linux操作系统（双语课）-网络231、232-张森"
    exit 1
fi 

# 配置

export set_score_fields="${SCORE_FIELDS:=1,2,10}"
sort_field=$(cut -d, -f 3 <<< "$set_score_fields")

# 从https://gitee.com/senzhanggit/doc_format_conv 下载或者用git 克隆到本地，
# 把对应的本地目录的路径设置为local_repos_path的值
local_repos_path=${LOCAL_TOOL_PATH:="H:/devworks/win_file_format_conv"}
unix_local_repos_path="$(cygpath -u -a $local_repos_path)"
export PATH=$PATH:$unix_local_repos_path

# 2022-2023学年第1学期_网络空间安全_Linux操作系统高级应用_网安201
archive_info="$1"
cd $archive_info
pwd

# if test ! -d "01.学生试卷" -o "$(ls -1 01.学生试卷 |wc -l)" = "0"; then
# fi

# 1. PPT->pdf
powershell.exe -ExecutionPolicy Bypass -File "${local_repos_path}/ConvertPowerpointTo-PDF.ps1"

# 2. Excel转pdf
powershell.exe -ExecutionPolicy Bypass -File "${local_repos_path}/ConvertExcelTo-PDF.ps1"

# 3. Word转pdf
powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "Import-Module ${local_repos_path}/ConvertWordTo-PDF.psm1; ConvertWordTo-PDF -SourceFolder ."

# 9. 整合pdf
output_pdf_filename="${archive_info}.pdf"
# qpdf --empty --pages $(find . -name "*.pdf" -printf "'%p'\n" |tr '\n' ' ') -- "../${output_pdf_filename}"
# eval $(echo qpdf --empty --pages $(find . -name "*.pdf" -exec printf "%q " {} \; )  -- "../${output_pdf_filename}")
# eval $(echo qpdf --empty --pages $(find . -name "*.pdf" -print0 |xargs -0 printf "%q " )  -- "../${output_pdf_filename}")
find . -type f -name '*.pdf' -printf "%p\0" | xargs -0 bash -c 'qpdf --empty --pages "$@" -- ../${output_pdf_filename}'


echo "生成完毕：" $(realpath "../${output_pdf_filename}")

cd -
